package com.bankwel.j3d.ui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import org.apache.log4j.Logger;
import com.bankwel.j3d.model.Object3d;

public final class J3dPanel extends JPanel {

	private Object3d obj;
	private Image image;
	private int width;
	private int height;

	public J3dPanel(Object3d obj, int width, int height) {
		super();
		this.obj = obj;
		this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		this.width = width;
		this.height = height;
		Logger.getLogger(J3dPanel.class).info("J3d Panel initialized with Object3d successfully");
	}

	private static final long serialVersionUID = 1L;

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) image.getGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.clearRect(0, 0, width, height);
		obj.draw(g2d);
		g2d.dispose();
		image.flush();
		g.drawImage(image, 0, 0, null);
	}
}
